package br.unb.mestrado.analysis;

import java.text.DecimalFormat;
import java.text.NumberFormat;

import br.unb.mestrado.simulator.control.impl.QualityFormulaResultControl;
import br.unb.mestrado.simulator.model.simulator.formula.QualityFormula;
import br.unb.mestrado.simulator.model.simulator.statistic.FeatureStatistic;
import br.unb.mestrado.simulator.model.simulator.statistic.FeatureStatisticState;
import br.unb.mestrado.simulator.model.simulator.statistic.FeaturesStatistic;
import br.unb.mestrado.simulator.model.simulator.statistic.QualityFormulaStatistic;
import br.unb.mestrado.simulator.model.spl.Feature;
import br.unb.mestrado.simulator.util.SimulatorException;

public class Analyser {

	
	public static void main(String[] args) {
		
		Analyser analyser = new Analyser();
		Writer writer = new Writer();
		try {
			analyser.createComparationAnalysis(writer);
		} catch (SimulatorException e) {
			e.printStackTrace();
		}
		
	}

	
	
	private void createComparationAnalysis(Writer writer) throws SimulatorException {

		QualityFormulaResultControl control = new QualityFormulaResultControl();
		QualityFormulaStatistic statistic = control.getStatistic();
		FeaturesStatistic featureStatistic = control.getFeatureStatistic();
		
		Analyser.writeStatistic(statistic, writer);
		Analyser.writeFeatureStatistic(featureStatistic, writer);
	}



	



	private static void writeFeatureStatistic(FeaturesStatistic featureStatistic, Writer writer) {
		if(featureStatistic!=null){

			
			String headers[]={"#Legenda","Feature","Total","Aprovado","Reprovado","Estado"};
			int max=-1;
			for (int j = 0; j < headers.length; j++) {
				if(headers[j].length()>max){
					max = headers[j].length();
				}
			} 
			writer.write(headers[0], max ,headers[1],headers[2],headers[3],headers[4],headers[5]);
			for (int i = 0; i < featureStatistic.getFeatures().size(); i++) {
				FeatureStatistic fs = featureStatistic.getFeatures().get(i);
				writeFeatureStatisticState(writer,max, fs.getFeature(), fs.getClazz(), fs.getStateN());
				writeFeatureStatisticState(writer,max,fs.getFeature(), fs.getClazz(), fs.getStateM());
				writeFeatureStatisticState(writer,max,fs.getFeature(), fs.getClazz(), fs.getStateH());
				writer.newLine();
			}
		}
	}



	private static void writeFeatureStatisticState(Writer writer, int max, Feature feature, Class<QualityFormula> clazz, FeatureStatisticState fss) {
		writer.write("#Analysis", max, feature.getName(), fss.getTotal(), fss.getApproved(), (fss.getTotal()-fss.getApproved()),fss.getState().name());
	}



	private static void writeStatistic(QualityFormulaStatistic statistic, Writer writer) {
		NumberFormat df = DecimalFormat.getInstance();
		df.setMaximumFractionDigits(1);
		df.setMinimumFractionDigits(1);

		writer.write("#Number of analysis: ",statistic.getNumberOfResult()+"");
		writer.write("#Number of analysis (GOA): ",statistic.getNumberOfResultGOAL()+"");
		writer.write("#Number of analysis (GOA APPROVED): ",statistic.getNumberOfResultGOALApproved()+"");
		writer.write("#Number of analysis (GOA REPROVED): ",statistic.getNumberOfResultGOALReproved()+"");
		writer.write("#Number of analysis (MCF): ",statistic.getNumberOfResultMCF()+"");
		writer.write("#Number of analysis (MCF APPROVED): ",statistic.getNumberOfResultMCFApproved()+"");
		writer.write("#Number of analysis (MCF REPPROVED): ",statistic.getNumberOfResultMCFRepproved()+"");
		writer.write("#Percentage of GOA approvement",""+df.format(statistic.getPercentageApprovementGOA()*100)+" %");
		writer.write("#Percentage of MCF approvement",""+df.format(statistic.getPercentageApprovementMCF()*100)+" %");
		writer.newLine();
		
		int max=-1;
		String headers[]={"#Legenda","Total","Goa-Total","GOA-Aprov.","GOA-Reprov.", "MCF-Total", "MCF-Aprov.", "MCF-Reprov"};
		for (int i = 0; i < headers.length; i++) {
			if(headers[i].length()>max){
				max = headers[i].length();
			}
		}
		writer.write(headers[0], max ,headers[1],headers[2],headers[3],headers[4],headers[5],headers[6],headers[7]);
		
		writer.write("#Valores", 
				 max ,
				statistic.getNumberOfResult(),
				statistic.getNumberOfResultGOAL(),
				statistic.getNumberOfResultGOALApproved(),
				statistic.getNumberOfResultGOALReproved(),
				statistic.getNumberOfResultMCF(),
				statistic.getNumberOfResultMCFApproved(),
				statistic.getNumberOfResultMCFRepproved());
		writer.newLine();
	}
	
}

class Writer{
	
	private static final String SEPARATOR = "\t";
	
	public void write(String identifier, String message){
		System.out.println(identifier+SEPARATOR+message);
	}
	
	public void newLine() {
		System.out.print("\n");
	}

	public void write(String identifier, int max, Object... message){
		StringBuilder sb = new StringBuilder();
		for (Object string : message) {
			while(string.toString().length()<max){
				string = " "+string;
			}
			sb.append(SEPARATOR+string.toString());
		}
		System.out.println(identifier+sb.toString());
	}
}
